#pragma once

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct
{
    unsigned int x;
    unsigned int y;
} uint64 ;

typedef struct
{
    unsigned int x;
    unsigned int y;
    unsigned int z;
    unsigned int w;
} uint128 ;

#define FNV_PRIME 0x01000193

#define fnv(x, y) ((x)*FNV_PRIME ^ (y))

uint128 fnv4(uint128 a, uint128 b)
{
    uint128 c;
    c.x = a.x * FNV_PRIME ^ b.x;
    c.y = a.y * FNV_PRIME ^ b.y;
    c.z = a.z * FNV_PRIME ^ b.z;
    c.w = a.w * FNV_PRIME ^ b.w;
    return c;
}

unsigned int fnv_reduce(uint128 v)
{
    int result;
    result = fnv(v.x, v.y);
    result = fnv(result, v.z);
    result = fnv(result, v.w);
    // return fnv(fnv(fnv(v.x, v.y), v.z), v.w);
    return result;
}

uint64 keccak_round_constants[24] = {
    { 0x00000001, 0x00000000 }, { 0x00008082, 0x00000000 }, { 0x0000808a, 0x80000000 }, { 0x80008000, 0x80000000 },
    { 0x0000808b, 0x00000000 }, { 0x80000001, 0x00000000 }, { 0x80008081, 0x80000000 }, { 0x00008009, 0x80000000 },
    { 0x0000008a, 0x00000000 }, { 0x00000088, 0x00000000 }, { 0x80008009, 0x00000000 }, { 0x8000000a, 0x00000000 },
    { 0x8000808b, 0x00000000 }, { 0x0000008b, 0x80000000 }, { 0x00008089, 0x80000000 }, { 0x00008003, 0x80000000 },
    { 0x00008002, 0x80000000 }, { 0x00000080, 0x80000000 }, { 0x0000800a, 0x00000000 }, { 0x8000000a, 0x80000000 },
    { 0x80008081, 0x80000000 }, { 0x00008080, 0x80000000 }, { 0x80000001, 0x00000000 }, { 0x80008008, 0x80000000 }
};


uint64 operator&(uint64 a, uint64 b){
    uint64 result;
    result.x = a.x & b.x; result.y = a.y & b.y;
    return result;
}

uint64 operator|(uint64 a, uint64 b){
    uint64 result;
    result.x = a.x | b.x; result.y = a.y | b.y;
    return result;
}

uint64 operator^(uint64 a, uint64 b){
    uint64 result;
    result.x = a.x ^ b.x; result.y = a.y ^ b.y;
    return result;
}

uint64 operator>>(uint64 a, int n){
    uint64 result;
    if(n>=32){
        result.x = a.y >> (n - 32);
        result.y = 0;
    }
    else
    {
        result.x = (a.x >> n) + (a.y << (32 - n) );
        result.y = a.y >> n;
    }
    return result;
}

uint64 operator<<(uint64 a, int n){
    uint64 result;
    if(n>=32){
        result.y = a.x << (n - 32);
        result.x = 0;
    }
    else
    {
        result.y = (a.y << n) + (a.x >> (32 - n) );
        result.x = a.x << n;
    }
    return result;
}

uint64 xor5(uint64 a, uint64 b, uint64 c, uint64 d, uint64 e){
    uint64 data;
    data.x = a.x ^ b.x ^ c.x ^ d.x ^ e.x;
    data.y = a.y ^ b.y ^ c.y ^ d.y ^ e.y;
    return data;
}

uint64 xor3(uint64 a, uint64 b, uint64 c){
    uint64 data;
    data.x = a.x ^ b.x ^ c.x;
    data.y = a.y ^ b.y ^ c.y;
    return data;
}

uint64 chi(uint64 a, uint64 b, uint64 c){
    uint64 data;
    data.x = a.x ^ (~b.x) & c.x;
    data.y = a.y ^ (~b.y) & c.y;
    return data;
}

unsigned int __byte_perm(unsigned int x, unsigned int y, unsigned int s){
    unsigned int result[4], input[8], selector[4];
    selector[0] = (s << 29) >> 29;  // s[2:0]
    selector[1] = (s << 25) >> 29;  // s[6:4]
    selector[2] = (s << 21) >> 29;  // s[10:8]
    selector[3] = (s << 17) >> 29;  // s[14:12]

    input[0] = (x << 24) >> 24;  // x[7:0]
    input[1] = (x << 16) >> 24;  // x[15:8]
    input[2] = (x << 8) >> 24;  // x[23:16]
    input[3] =  x >> 24;  // x[31:24]
    input[4] = (y << 24) >> 24;  // y[7:0]
    input[5] = (y << 16) >> 24;  // y[15:8]
    input[6] = (y << 8) >> 24;  // y[23:16]
    input[7] =  y >> 24;  // y[31:24]

    for(int i=0; i<4; i++){
        result[i] = input[selector[i]];
    }
    unsigned int results = (result[3] << 24) + (result[2] << 16) + (result[1] << 8) + result[0];

    return results;
}

uint64 ROL2(uint64 v, int n){
    uint64 result;
    if (n <= 32)
    {
        result.y = ((v.y << (n)) | (v.x >> (32 - n)));
        result.x = ((v.x << (n)) | (v.y >> (32 - n)));
    }
    else
    {
        result.y = ((v.x << (n - 32)) | (v.y >> (64 - n)));
        result.x = ((v.y << (n - 32)) | (v.x >> (64 - n)));
    }
    return result;
}

uint64 ROL8(uint64 a){
    uint64 result;
    result.x = __byte_perm(a.y, a.x, 0x6543);
    result.y = __byte_perm(a.y, a.x, 0x2107);
    
    return result;
}

uint64 ROR8(uint64 a){
    uint64 result;
    result.x = __byte_perm(a.y, a.x, 0x0765);
    result.y = __byte_perm(a.y, a.x, 0x4321);
    
    return result;
}

uint64 cuda_swab64(uint64 x){
    uint64 result;
    uint64 swap[8];
    swap[0].x = 0x00000000; swap[0].y = 0xff000000;   //0xff00000000000000ULL  
    swap[1].x = 0x00000000; swap[1].y = 0x00ff0000;   //0x00ff000000000000ULL
    swap[2].x = 0x00000000; swap[2].y = 0x0000ff00;   //0x0000ff0000000000ULL
    swap[3].x = 0x00000000; swap[3].y = 0x000000ff;   //0x000000ff00000000ULL
    swap[4].x = 0xff000000; swap[4].y = 0x00000000;   //0x00000000ff000000ULL
    swap[5].x = 0x00ff0000; swap[5].y = 0x00000000;   //0x0000000000ff0000ULL
    swap[6].x = 0x0000ff00; swap[6].y = 0x00000000;   //0x000000000000ff00ULL
    swap[7].x = 0x000000ff; swap[7].y = 0x00000000;   //0x00000000000000ffULL

    result = (  
                ( ( x & swap[0]) >> 56) | \
                ( ( x & swap[1]) >> 40) | \
                ( ( x & swap[2]) >> 24) | \
                ( ( x & swap[3]) >> 8 ) |  \
                ( ( x & swap[4]) << 8 ) |  \
                ( ( x & swap[5]) << 24) | \
                ( ( x & swap[6]) << 40) | \
                ( ( x & swap[7]) << 56)    
             );
    return result;
}

bool operator>(uint64 a, uint64 b){

    if(a.y > b.y){
        return true;
    }
    else if(a.y == b.y){
      if(a.x > b.x){
          return true;
      }
      else{
          return false;
      }
    }
    else{
        return false;
    }
}

void keccak_f1600_init(uint64* state){
    uint64 s[25];
    uint64 t[5], u, v;

    for(int i=0; i<5 ; i++)
    {
        s[i].x = state[i].x;
        s[i].y = state[i].y;
    }
    s[5].x = 0x00000001; s[5].y = 0x00000000;
    s[6].x = 0x00000000; s[6].y = 0x00000000;
    s[7].x = 0x00000000; s[7].y = 0x00000000;
    s[8].x = 0x00000000; s[8].y = 0x80000000;
    for (int i = 9; i < 25; i++)
    {
        s[i].x = 0x00000000; s[i].y = 0x00000000;
    }

    /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
    t[0].x = s[0].x ^ s[5].x;
    t[0].y = s[0].y;
    t[1] = s[1];
    t[2] = s[2];
    t[3].x = s[3].x;
    t[3].y = s[3].y ^ s[8].y;
    t[4] = s[4];

    /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
    /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */

    u = t[4] ^ ROL2(t[1], 1);
    s[0] = s[0] ^ u;
    s[5] = s[5] ^ u;
    s[10] = s[10] ^ u;
    s[15] = s[15] ^ u;
    s[20] = s[20] ^ u;

    u = t[0] ^ ROL2(t[2], 1);
    s[1] = s[1] ^ u;
    s[6] = s[6] ^ u;
    s[11] = s[11] ^ u;
    s[16] = s[16] ^ u;
    s[21] = s[21] ^ u;

    u = t[1] ^ ROL2(t[3], 1);
    s[2] = s[2] ^ u;
    s[7] = s[7] ^ u;
    s[12] = s[12] ^ u;
    s[17] = s[17] ^ u;
    s[22] = s[22] ^ u;

    u = t[2] ^ ROL2(t[4], 1);
    s[3] = s[3] ^ u;
    s[8] = s[8] ^ u;
    s[13] = s[13] ^ u;
    s[18] = s[18] ^ u;
    s[23] = s[23] ^ u;

    u = t[3] ^ ROL2(t[0], 1);
    s[4] = s[4] ^ u;
    s[9] = s[9] ^ u;
    s[14] = s[14] ^ u;
    s[19] = s[19] ^ u;
    s[24] = s[24] ^ u;

    /* rho pi: b[..] = rotl(a[..], ..) */
    u = s[1];

    s[1] = ROL2(s[6], 44);
    s[6] = ROL2(s[9], 20);
    s[9] = ROL2(s[22], 61);
    s[22] = ROL2(s[14], 39);
    s[14] = ROL2(s[20], 18);
    s[20] = ROL2(s[2], 62);
    s[2] = ROL2(s[12], 43);
    s[12] = ROL2(s[13], 25);

    s[13] = ROL8(s[19]);
    s[19] = ROR8(s[23]);

    s[23] = ROL2(s[15], 41);
    s[15] = ROL2(s[4], 27);
    s[4] = ROL2(s[24], 14);
    s[24] = ROL2(s[21], 2);
    s[21] = ROL2(s[8], 55);
    s[8] = ROL2(s[16], 45);
    s[16] = ROL2(s[5], 36);
    s[5] = ROL2(s[3], 28);
    s[3] = ROL2(s[18], 21);
    s[18] = ROL2(s[17], 15);
    s[17] = ROL2(s[11], 10);
    s[11] = ROL2(s[7], 6);
    s[7] = ROL2(s[10], 3);
    s[10] = ROL2(u, 1);

    /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */

    u = s[0];
    v = s[1];
    s[0] = chi(s[0], s[1], s[2]);
    s[1] = chi(s[1], s[2], s[3]);
    s[2] = chi(s[2], s[3], s[4]);
    s[3] = chi(s[3], s[4], u);
    s[4] = chi(s[4], u, v);

    u = s[5];
    v = s[6];
    s[5] = chi(s[5], s[6], s[7]);
    s[6] = chi(s[6], s[7], s[8]);
    s[7] = chi(s[7], s[8], s[9]);
    s[8] = chi(s[8], s[9], u);
    s[9] = chi(s[9], u, v);

    u = s[10];
    v = s[11];
    s[10] = chi(s[10], s[11], s[12]);
    s[11] = chi(s[11], s[12], s[13]);
    s[12] = chi(s[12], s[13], s[14]);
    s[13] = chi(s[13], s[14], u);
    s[14] = chi(s[14], u, v);

    u = s[15];
    v = s[16];
    s[15] = chi(s[15], s[16], s[17]);
    s[16] = chi(s[16], s[17], s[18]);
    s[17] = chi(s[17], s[18], s[19]);
    s[18] = chi(s[18], s[19], u);
    s[19] = chi(s[19], u, v);

    u = s[20];
    v = s[21];
    s[20] = chi(s[20], s[21], s[22]);
    s[21] = chi(s[21], s[22], s[23]);
    s[22] = chi(s[22], s[23], s[24]);
    s[23] = chi(s[23], s[24], u);
    s[24] = chi(s[24], u, v);

    /* iota: a[0,0] ^= round constant */
    s[0] = s[0] ^ keccak_round_constants[0];

    for (int i = 1; i < 23; i++)
    {
        /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
        t[0] = xor5(s[0], s[5], s[10], s[15], s[20]);
        t[1] = xor5(s[1], s[6], s[11], s[16], s[21]);
        t[2] = xor5(s[2], s[7], s[12], s[17], s[22]);
        t[3] = xor5(s[3], s[8], s[13], s[18], s[23]);
        t[4] = xor5(s[4], s[9], s[14], s[19], s[24]);

        /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
        /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */

        u = t[4] ^ ROL2(t[1], 1);
        s[0] = s[0] ^ u;
        s[5] = s[5] ^ u;
        s[10] = s[10] ^ u;
        s[15] = s[15] ^ u;
        s[20] = s[20] ^ u;

        u = t[0] ^ ROL2(t[2], 1);
        s[1] = s[1] ^ u;
        s[6] = s[6] ^ u;
        s[11] = s[11] ^ u;
        s[16] = s[16] ^ u;
        s[21] = s[21] ^ u;

        u = t[1] ^ ROL2(t[3], 1);
        s[2] = s[2] ^ u;
        s[7] = s[7] ^ u;
        s[12] = s[12] ^ u;
        s[17] = s[17] ^ u;
        s[22] = s[22] ^ u;

        u = t[2] ^ ROL2(t[4], 1);
        s[3] = s[3] ^ u;
        s[8] = s[8] ^ u;
        s[13] = s[13] ^ u;
        s[18] = s[18] ^ u;
        s[23] = s[23] ^ u;

        u = t[3] ^ ROL2(t[0], 1);
        s[4] = s[4] ^ u;
        s[9] = s[9] ^ u;
        s[14] = s[14] ^ u;
        s[19] = s[19] ^ u;
        s[24] = s[24] ^ u;

        /* rho pi: b[..] = rotl(a[..], ..) */
        u = s[1];

        s[1] = ROL2(s[6], 44);
        s[6] = ROL2(s[9], 20);
        s[9] = ROL2(s[22], 61);
        s[22] = ROL2(s[14], 39);
        s[14] = ROL2(s[20], 18);
        s[20] = ROL2(s[2], 62);
        s[2] = ROL2(s[12], 43);
        s[12] = ROL2(s[13], 25);
        s[13] = ROL8(s[19]);
        s[19] = ROR8(s[23]);
        s[23] = ROL2(s[15], 41);
        s[15] = ROL2(s[4], 27);
        s[4] = ROL2(s[24], 14);
        s[24] = ROL2(s[21], 2);
        s[21] = ROL2(s[8], 55);
        s[8] = ROL2(s[16], 45);
        s[16] = ROL2(s[5], 36);
        s[5] = ROL2(s[3], 28);
        s[3] = ROL2(s[18], 21);
        s[18] = ROL2(s[17], 15);
        s[17] = ROL2(s[11], 10);
        s[11] = ROL2(s[7], 6);
        s[7] = ROL2(s[10], 3);
        s[10] = ROL2(u, 1);

        /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */

        u = s[0];
        v = s[1];
        s[0] = chi(s[0], s[1], s[2]);
        s[1] = chi(s[1], s[2], s[3]);
        s[2] = chi(s[2], s[3], s[4]);
        s[3] = chi(s[3], s[4], u);
        s[4] = chi(s[4], u, v);

        u = s[5];
        v = s[6];
        s[5] = chi(s[5], s[6], s[7]);
        s[6] = chi(s[6], s[7], s[8]);
        s[7] = chi(s[7], s[8], s[9]);
        s[8] = chi(s[8], s[9], u);
        s[9] = chi(s[9], u, v);

        u = s[10];
        v = s[11];
        s[10] = chi(s[10], s[11], s[12]);
        s[11] = chi(s[11], s[12], s[13]);
        s[12] = chi(s[12], s[13], s[14]);
        s[13] = chi(s[13], s[14], u);
        s[14] = chi(s[14], u, v);

        u = s[15];
        v = s[16];
        s[15] = chi(s[15], s[16], s[17]);
        s[16] = chi(s[16], s[17], s[18]);
        s[17] = chi(s[17], s[18], s[19]);
        s[18] = chi(s[18], s[19], u);
        s[19] = chi(s[19], u, v);

        u = s[20];
        v = s[21];
        s[20] = chi(s[20], s[21], s[22]);
        s[21] = chi(s[21], s[22], s[23]);
        s[22] = chi(s[22], s[23], s[24]);
        s[23] = chi(s[23], s[24], u);
        s[24] = chi(s[24], u, v);

        /* iota: a[0,0] ^= round constant */
        s[0] = s[0] ^ keccak_round_constants[i];
    
    }

    /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
    t[0] = xor5(s[0], s[5], s[10], s[15], s[20]);
    t[1] = xor5(s[1], s[6], s[11], s[16], s[21]);
    t[2] = xor5(s[2], s[7], s[12], s[17], s[22]);
    t[3] = xor5(s[3], s[8], s[13], s[18], s[23]);
    t[4] = xor5(s[4], s[9], s[14], s[19], s[24]);

    /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
    /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */

    u = t[4] ^ ROL2(t[1], 1);
    s[0] = s[0] ^ u;
    s[10] = s[10] ^ u;

    u = t[0] ^ ROL2(t[2], 1);
    s[6] = s[6] ^ u;
    s[16] = s[16] ^ u;

    u = t[1] ^ ROL2(t[3], 1);
    s[12] = s[12] ^ u;
    s[22] = s[22] ^ u;

    u = t[2] ^ ROL2(t[4], 1);
    s[3] = s[3] ^ u;
    s[18] = s[18] ^ u;

    u = t[3] ^ ROL2(t[0], 1);
    s[9] = s[9] ^ u;
    s[24] = s[24] ^ u;

    /* rho pi: b[..] = rotl(a[..], ..) */
    u = s[1];

    s[1] = ROL2(s[6], 44);
    s[6] = ROL2(s[9], 20);
    s[9] = ROL2(s[22], 61);
    s[2] = ROL2(s[12], 43);
    s[4] = ROL2(s[24], 14);
    s[8] = ROL2(s[16], 45);
    s[5] = ROL2(s[3], 28);
    s[3] = ROL2(s[18], 21);
    s[7] = ROL2(s[10], 3);

    /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */

    u = s[0];
    v = s[1];
    s[0] = chi(s[0], s[1], s[2]);
    s[1] = chi(s[1], s[2], s[3]);
    s[2] = chi(s[2], s[3], s[4]);
    s[3] = chi(s[3], s[4], u);
    s[4] = chi(s[4], u, v);
    s[5] = chi(s[5], s[6], s[7]);
    s[6] = chi(s[6], s[7], s[8]);
    s[7] = chi(s[7], s[8], s[9]);

    /* iota: a[0,0] ^= round constant */
    s[0] = s[0] ^ keccak_round_constants[23];

     for (int i = 0; i < 12; ++i){
        state[i].x = s[i].x;
        state[i].y = s[i].y;
     }
}

uint64 keccak_f1600_final(uint64* state){
    uint64 s[25];
    uint64 t[5], u, v;

    for (int i = 0; i < 12; i++){
        s[i] = state[i];
    }
    s[12].x = 0x00000001; s[12].y = 0x00000000;
    s[13].x = 0x00000000; s[13].y = 0x00000000;
    s[14].x = 0x00000000; s[14].y = 0x00000000;
    s[15].x = 0x00000000; s[15].y = 0x00000000;
    s[16].x = 0x00000000; s[16].y = 0x80000000;
    for (int i = 17; i < 25; i++){
        s[i].x = 0x00000000; s[i].y = 0x00000000;
    }

    /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
    t[0] = xor3(s[0], s[5], s[10]);
    t[1] = xor3(s[1], s[6], s[11]) ^ s[16];
    t[2] = xor3(s[2], s[7], s[12]);
    t[3] = s[3] ^ s[8];
    t[4] = s[4] ^ s[9];

    /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
    /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */

    u = t[4] ^ ROL2(t[1], 1);
    s[0] = s[0] ^ u;
    s[5] = s[5] ^ u;
    s[10] = s[10] ^ u;
    s[15] = s[15] ^ u;
    s[20] = s[20] ^ u;

    u = t[0] ^ ROL2(t[2], 1);
    s[1] = s[1] ^ u;
    s[6] = s[6] ^ u;
    s[11] = s[11] ^ u;
    s[16] = s[16] ^ u;
    s[21] = s[21] ^ u;

    u = t[1] ^ ROL2(t[3], 1);
    s[2] = s[2] ^ u;
    s[7] = s[7] ^ u;
    s[12] = s[12] ^ u;
    s[17] = s[17] ^ u;
    s[22] = s[22] ^ u;

    u = t[2] ^ ROL2(t[4], 1);
    s[3] = s[3] ^ u;
    s[8] = s[8] ^ u;
    s[13] = s[13] ^ u;
    s[18] = s[18] ^ u;
    s[23] = s[23] ^ u;

    u = t[3] ^ ROL2(t[0], 1);
    s[4] = s[4] ^ u;
    s[9] = s[9] ^ u;
    s[14] = s[14] ^ u;
    s[19] = s[19] ^ u;
    s[24] = s[24] ^ u; 

    /* rho pi: b[..] = rotl(a[..], ..) */
    u = s[1];

    s[1] = ROL2(s[6], 44);
    s[6] = ROL2(s[9], 20);
    s[9] = ROL2(s[22], 61);
    s[22] = ROL2(s[14], 39);
    s[14] = ROL2(s[20], 18);
    s[20] = ROL2(s[2], 62);
    s[2] = ROL2(s[12], 43);
    s[12] = ROL2(s[13], 25);
    s[13] = ROL8(s[19]);
    s[19] = ROR8(s[23]);
    s[23] = ROL2(s[15], 41);
    s[15] = ROL2(s[4], 27);
    s[4] = ROL2(s[24], 14);
    s[24] = ROL2(s[21], 2);
    s[21] = ROL2(s[8], 55);
    s[8] = ROL2(s[16], 45);
    s[16] = ROL2(s[5], 36);
    s[5] = ROL2(s[3], 28);
    s[3] = ROL2(s[18], 21);
    s[18] = ROL2(s[17], 15);
    s[17] = ROL2(s[11], 10);
    s[11] = ROL2(s[7], 6);
    s[7] = ROL2(s[10], 3);
    s[10] = ROL2(u, 1);  

    /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */
    u = s[0];
    v = s[1];
    s[0] = chi(s[0], s[1], s[2]);
    s[1] = chi(s[1], s[2], s[3]);
    s[2] = chi(s[2], s[3], s[4]);
    s[3] = chi(s[3], s[4], u);
    s[4] = chi(s[4], u, v);

    u = s[5];
    v = s[6];
    s[5] = chi(s[5], s[6], s[7]);
    s[6] = chi(s[6], s[7], s[8]);
    s[7] = chi(s[7], s[8], s[9]);
    s[8] = chi(s[8], s[9], u);
    s[9] = chi(s[9], u, v);

    u = s[10];
    v = s[11];
    s[10] = chi(s[10], s[11], s[12]);
    s[11] = chi(s[11], s[12], s[13]);
    s[12] = chi(s[12], s[13], s[14]);
    s[13] = chi(s[13], s[14], u);
    s[14] = chi(s[14], u, v);

    u = s[15];
    v = s[16];
    s[15] = chi(s[15], s[16], s[17]);
    s[16] = chi(s[16], s[17], s[18]);
    s[17] = chi(s[17], s[18], s[19]);
    s[18] = chi(s[18], s[19], u);
    s[19] = chi(s[19], u, v);

    u = s[20];
    v = s[21];
    s[20] = chi(s[20], s[21], s[22]);
    s[21] = chi(s[21], s[22], s[23]);
    s[22] = chi(s[22], s[23], s[24]);
    s[23] = chi(s[23], s[24], u);
    s[24] = chi(s[24], u, v);

    /* iota: a[0,0] ^= round constant */
    s[0] = s[0] ^ keccak_round_constants[0];

    for (int i = 1; i < 23; i++)
    {
        /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
        t[0] = xor5(s[0], s[5], s[10], s[15], s[20]);
        t[1] = xor5(s[1], s[6], s[11], s[16], s[21]);
        t[2] = xor5(s[2], s[7], s[12], s[17], s[22]);
        t[3] = xor5(s[3], s[8], s[13], s[18], s[23]);
        t[4] = xor5(s[4], s[9], s[14], s[19], s[24]);

        /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
        /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */

        u = t[4] ^ ROL2(t[1], 1);
        s[0] = s[0] ^ u;
        s[5] = s[5] ^ u;
        s[10] = s[10] ^ u;
        s[15] = s[15] ^ u;
        s[20] = s[20] ^ u;

        u = t[0] ^ ROL2(t[2], 1);
        s[1] = s[1] ^ u;
        s[6] = s[6] ^ u;
        s[11] = s[11] ^ u;
        s[16] = s[16] ^ u;
        s[21] = s[21] ^ u;

        u = t[1] ^ ROL2(t[3], 1);
        s[2] = s[2] ^ u;
        s[7] = s[7] ^ u;
        s[12] = s[12] ^ u;
        s[17] = s[17] ^ u;
        s[22] = s[22] ^ u;

        u = t[2] ^ ROL2(t[4], 1);
        s[3] = s[3] ^ u;
        s[8] = s[8] ^ u;
        s[13] = s[13] ^ u;
        s[18] = s[18] ^ u;
        s[23] = s[23] ^ u;

        u = t[3] ^ ROL2(t[0], 1);
        s[4] = s[4] ^ u;
        s[9] = s[9] ^ u;
        s[14] = s[14] ^ u;
        s[19] = s[19] ^ u;
        s[24] = s[24] ^ u;

        /* rho pi: b[..] = rotl(a[..], ..) */
        u = s[1];

        s[1] = ROL2(s[6], 44);
        s[6] = ROL2(s[9], 20);
        s[9] = ROL2(s[22], 61);
        s[22] = ROL2(s[14], 39);
        s[14] = ROL2(s[20], 18);
        s[20] = ROL2(s[2], 62);
        s[2] = ROL2(s[12], 43);
        s[12] = ROL2(s[13], 25);
        s[13] = ROL8(s[19]);
        s[19] = ROR8(s[23]);
        s[23] = ROL2(s[15], 41);
        s[15] = ROL2(s[4], 27);
        s[4] = ROL2(s[24], 14);
        s[24] = ROL2(s[21], 2);
        s[21] = ROL2(s[8], 55);
        s[8] = ROL2(s[16], 45);
        s[16] = ROL2(s[5], 36);
        s[5] = ROL2(s[3], 28);
        s[3] = ROL2(s[18], 21);
        s[18] = ROL2(s[17], 15);
        s[17] = ROL2(s[11], 10);
        s[11] = ROL2(s[7], 6);
        s[7] = ROL2(s[10], 3);
        s[10] = ROL2(u, 1);

        /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */
        u = s[0];
        v = s[1];
        s[0] = chi(s[0], s[1], s[2]);
        s[1] = chi(s[1], s[2], s[3]);
        s[2] = chi(s[2], s[3], s[4]);
        s[3] = chi(s[3], s[4], u);
        s[4] = chi(s[4], u, v);

        u = s[5];
        v = s[6];
        s[5] = chi(s[5], s[6], s[7]);
        s[6] = chi(s[6], s[7], s[8]);
        s[7] = chi(s[7], s[8], s[9]);
        s[8] = chi(s[8], s[9], u);
        s[9] = chi(s[9], u, v);

        u = s[10];
        v = s[11];
        s[10] = chi(s[10], s[11], s[12]);
        s[11] = chi(s[11], s[12], s[13]);
        s[12] = chi(s[12], s[13], s[14]);
        s[13] = chi(s[13], s[14], u);
        s[14] = chi(s[14], u, v);

        u = s[15];
        v = s[16];
        s[15] = chi(s[15], s[16], s[17]);
        s[16] = chi(s[16], s[17], s[18]);
        s[17] = chi(s[17], s[18], s[19]);
        s[18] = chi(s[18], s[19], u);
        s[19] = chi(s[19], u, v);

        u = s[20];
        v = s[21];
        s[20] = chi(s[20], s[21], s[22]);
        s[21] = chi(s[21], s[22], s[23]);
        s[22] = chi(s[22], s[23], s[24]);
        s[23] = chi(s[23], s[24], u);
        s[24] = chi(s[24], u, v);

        /* iota: a[0,0] ^= round constant */
        s[0] = s[0] ^ keccak_round_constants[i];
    }

    t[0] = xor5(s[0], s[5], s[10], s[15], s[20]);
    t[1] = xor5(s[1], s[6], s[11], s[16], s[21]);
    t[2] = xor5(s[2], s[7], s[12], s[17], s[22]);
    t[3] = xor5(s[3], s[8], s[13], s[18], s[23]);
    t[4] = xor5(s[4], s[9], s[14], s[19], s[24]);

    s[0] = xor3(s[0], t[4], ROL2(t[1], 1));
    s[6] = xor3(s[6], t[0], ROL2(t[2], 1));
    s[12] = xor3(s[12], t[1], ROL2(t[3], 1));

    s[1] = ROL2(s[6], 44);
    s[2] = ROL2(s[12], 43);

    s[0] = chi(s[0], s[1], s[2]);

    uint64 s_result = s[0] ^ keccak_round_constants[23];

    return s_result;

}

void SHA3_512(uint64* s)
{
    uint64 t[5], u, v;

    for (int i = 8; i < 25; i++)
    {
        s[i].x = 0x00000000;
        s[i].y = 0x00000000;
    }

    s[8].x = 0x00000001;
    s[8].y = 0x80000000;

    for (int i = 0; i < 23; i++)
    {
        /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
        t[0] = xor5(s[0], s[5], s[10], s[15], s[20]);
        t[1] = xor5(s[1], s[6], s[11], s[16], s[21]);
        t[2] = xor5(s[2], s[7], s[12], s[17], s[22]);
        t[3] = xor5(s[3], s[8], s[13], s[18], s[23]);
        t[4] = xor5(s[4], s[9], s[14], s[19], s[24]);

        /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
        /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */

        u = t[4] ^ ROL2(t[1], 1);
        s[0] = s[0] ^ u;
        s[5] = s[5] ^ u;
        s[10] = s[10] ^ u;
        s[15] = s[15] ^ u;
        s[20] = s[20] ^ u;

        u = t[0] ^ ROL2(t[2], 1);
        s[1] = s[1] ^ u;
        s[6] = s[6] ^ u;
        s[11] = s[11] ^ u;
        s[16] = s[16] ^ u;
        s[21] = s[21] ^ u;

        u = t[1] ^ ROL2(t[3], 1);
        s[2] = s[2] ^ u;
        s[7] = s[7] ^ u;
        s[12] = s[12] ^ u;
        s[17] = s[17] ^ u;
        s[22] = s[22] ^ u;

        u = t[2] ^ ROL2(t[4], 1);
        s[3] = s[3] ^ u;
        s[8] = s[8] ^ u;
        s[13] = s[13] ^ u;
        s[18] = s[18] ^ u;
        s[23] = s[23] ^ u;

        u = t[3] ^ ROL2(t[0], 1);
        s[4] = s[4] ^ u;
        s[9] = s[9] ^ u;
        s[14] = s[14] ^ u;
        s[19] = s[19] ^ u;
        s[24] = s[24] ^ u;

        /* rho pi: b[..] = rotl(a[..], ..) */
        u = s[1];

        s[1] = ROL2(s[6], 44);
        s[6] = ROL2(s[9], 20);
        s[9] = ROL2(s[22], 61);
        s[22] = ROL2(s[14], 39);
        s[14] = ROL2(s[20], 18);
        s[20] = ROL2(s[2], 62);
        s[2] = ROL2(s[12], 43);
        s[12] = ROL2(s[13], 25);
        s[13] = ROL2(s[19], 8);
        s[19] = ROL2(s[23], 56);
        s[23] = ROL2(s[15], 41);
        s[15] = ROL2(s[4], 27);
        s[4] = ROL2(s[24], 14);
        s[24] = ROL2(s[21], 2);
        s[21] = ROL2(s[8], 55);
        s[8] = ROL2(s[16], 45);
        s[16] = ROL2(s[5], 36);
        s[5] = ROL2(s[3], 28);
        s[3] = ROL2(s[18], 21);
        s[18] = ROL2(s[17], 15);
        s[17] = ROL2(s[11], 10);
        s[11] = ROL2(s[7], 6);
        s[7] = ROL2(s[10], 3);
        s[10] = ROL2(u, 1);

        /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */
        u = s[0];
        v = s[1];
        s[0] = chi(s[0], s[1], s[2]);
        s[1] = chi(s[1], s[2], s[3]);
        s[2] = chi(s[2], s[3], s[4]);
        s[3] = chi(s[3], s[4], u);
        s[4] = chi(s[4], u, v);

        u = s[5];
        v = s[6];
        s[5] = chi(s[5], s[6], s[7]);
        s[6] = chi(s[6], s[7], s[8]);
        s[7] = chi(s[7], s[8], s[9]);
        s[8] = chi(s[8], s[9], u);
        s[9] = chi(s[9], u, v);

        u = s[10];
        v = s[11];
        s[10] = chi(s[10], s[11], s[12]);
        s[11] = chi(s[11], s[12], s[13]);
        s[12] = chi(s[12], s[13], s[14]);
        s[13] = chi(s[13], s[14], u);
        s[14] = chi(s[14], u, v);

        u = s[15];
        v = s[16];
        s[15] = chi(s[15], s[16], s[17]);
        s[16] = chi(s[16], s[17], s[18]);
        s[17] = chi(s[17], s[18], s[19]);
        s[18] = chi(s[18], s[19], u);
        s[19] = chi(s[19], u, v);

        u = s[20];
        v = s[21];
        s[20] = chi(s[20], s[21], s[22]);
        s[21] = chi(s[21], s[22], s[23]);
        s[22] = chi(s[22], s[23], s[24]);
        s[23] = chi(s[23], s[24], u);
        s[24] = chi(s[24], u, v);

        /* iota: a[0,0] ^= round constant */
        s[0] = s[0] ^ keccak_round_constants[i];
    }

    /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
    t[0] = xor5(s[0], s[5], s[10], s[15], s[20]);
    t[1] = xor5(s[1], s[6], s[11], s[16], s[21]);
    t[2] = xor5(s[2], s[7], s[12], s[17], s[22]);
    t[3] = xor5(s[3], s[8], s[13], s[18], s[23]);
    t[4] = xor5(s[4], s[9], s[14], s[19], s[24]);

    /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
    /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */

    u = t[4] ^ ROL2(t[1], 1);
    s[0]  = s[0] ^ u;
    s[10] = s[10] ^ u;

    u = t[0] ^ ROL2(t[2], 1);
    s[6]  = s[6] ^ u;
    s[16] = s[16] ^ u;

    u = t[1] ^ ROL2(t[3], 1);
    s[12] = s[12] ^ u;
    s[22] = s[22] ^ u;

    u = t[2] ^ ROL2(t[4], 1);
    s[3]  = s[3] ^ u;
    s[18] = s[18] ^ u;

    u = t[3] ^ ROL2(t[0], 1);
    s[9]  = s[9] ^ u;
    s[24] = s[24] ^ u;

    /* rho pi: b[..] = rotl(a[..], ..) */
    u = s[1];

    s[1] = ROL2(s[6], 44);
    s[6] = ROL2(s[9], 20);
    s[9] = ROL2(s[22], 61);
    s[2] = ROL2(s[12], 43);
    s[4] = ROL2(s[24], 14);
    s[8] = ROL2(s[16], 45);
    s[5] = ROL2(s[3], 28);
    s[3] = ROL2(s[18], 21);
    s[7] = ROL2(s[10], 3);

    /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */

    u = s[0];
    v = s[1];
    s[0] = chi(s[0], s[1], s[2]);
    s[1] = chi(s[1], s[2], s[3]);
    s[2] = chi(s[2], s[3], s[4]);
    s[3] = chi(s[3], s[4], u);
    s[4] = chi(s[4], u, v);
    s[5] = chi(s[5], s[6], s[7]);
    s[6] = chi(s[6], s[7], s[8]);
    s[7] = chi(s[7], s[8], s[9]);

    /* iota: a[0,0] ^= round constant */
    s[0] = s[0] ^ keccak_round_constants[23];
}

#ifdef __cplusplus
}
#endif